package drugdealer.persistencia;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Vector;

import drugdealer.dados.Remedio;
import drugdealer.persistencia.exceptions.AlreadyExistsException;
import drugdealer.persistencia.exceptions.DoesNotExistsException;

public class RemedioPersistence extends TablePersistence implements IRemedioPersistence {
	public static String TABLENAME = "remedio";
	public static String PRIMARYKEY = "codigo";
	
	public RemedioPersistence(DDPersistence owner) {
		super(owner);
		this.tableName = RemedioPersistence.TABLENAME;
		this.primaryKey = RemedioPersistence.PRIMARYKEY;
		this.createTableStmt =
			"CREATE TABLE IF NOT EXISTS " + RemedioPersistence.TABLENAME + " (" +
			RemedioPersistence.PRIMARYKEY + " integer NOT NULL PRIMARY KEY," +
			"nome varchar(50)," +
			"laboratorio varchar(50)" +
			")";
	}
	
	/* (non-Javadoc)
	 * @see drugdealer.persistencia.IRemedioPersistence#get(int)
	 */
	public Remedio get(int codigo) {
		Remedio remedio = null;
		String selectRemedios = "SELECT * FROM " + RemedioPersistence.TABLENAME + 
			" WHERE codigo=" + codigo;
		
		System.out.println(selectRemedios);
		
		try {
			Statement s = this.ddc.createStatement();
			ResultSet rs = s.executeQuery(selectRemedios);
			if (rs.next()) {
				remedio = new Remedio(codigo, rs.getString("nome"),
						rs.getString("laboratorio"));
			}
			rs.close ();
			s.close ();
		} catch (SQLException e) {
			e.printStackTrace();
		}

		return remedio;
	}
	
	/* (non-Javadoc)
	 * @see drugdealer.persistencia.IRemedioPersistence#getList(java.lang.String)
	 */
	public Vector<String[]> getList(String columnValue) {
		return super.getList(
				RemedioPersistence.PRIMARYKEY, "nome", columnValue);
	}
	
	/* (non-Javadoc)
	 * @see drugdealer.persistencia.IRemedioPersistence#getRemedios()
	 */
	public Vector<Remedio> getRemedios(){
		Vector<Remedio> remedios = new Vector<Remedio>();
		String selectRemedios = "SELECT * FROM " + RemedioPersistence.TABLENAME;

		try {
			Statement s = this.ddc.createStatement();
			ResultSet rs = s.executeQuery(selectRemedios);
			while (rs.next())
				remedios.add(new Remedio(rs.getInt(RemedioPersistence.PRIMARYKEY),
						rs.getString("nome"), rs.getString("laboratorio")));
			rs.close ();
			s.close ();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return remedios;
	}
	
	/* (non-Javadoc)
	 * @see drugdealer.persistencia.IRemedioPersistence#getLaboratorios()
	 */
	public String[] getLaboratorios() {
		Vector<String> labsVec = new Vector<String>();
		String[] labs = null;
		
		String selectRemedios =
			"SELECT DISTINCT laboratorio FROM " + 
			RemedioPersistence.TABLENAME;

		try {
			Statement s = this.ddc.createStatement();
			ResultSet rs = s.executeQuery(selectRemedios);
			while (rs.next())
				labsVec.add(rs.getString("laboratorio"));
			labs = new String[labsVec.size()];
			labs = labsVec.toArray(labs);
			rs.close ();
			s.close ();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return labs;
	}
	
	/* (non-Javadoc)
	 * @see drugdealer.persistencia.IRemedioPersistence#add(drugdealer.dados.Remedio)
	 */
	public void add(Remedio remedio) throws AlreadyExistsException{
		super.add("INSERT INTO " + RemedioPersistence.TABLENAME + " VALUES (" +
				remedio.getCodigo() + ", '" + remedio.getNome() + "' , '" +
				remedio.getLaboratorio() + "')");
	}
	
	/* (non-Javadoc)
	 * @see drugdealer.persistencia.IRemedioPersistence#update(drugdealer.dados.Remedio)
	 */
	public void update(Remedio remedio) throws DoesNotExistsException {
		super.update("UPDATE " + RemedioPersistence.TABLENAME + " SET nome='" + 
				remedio.getNome() + "', " + "laboratorio='" +
				remedio.getLaboratorio() + "' " + "WHERE codigo=" +
				remedio.getCodigo());
	}
	
	/* (non-Javadoc)
	 * @see drugdealer.persistencia.IRemedioPersistence#remove(drugdealer.dados.Remedio)
	 */
	public void remove(Remedio remedio) {
		super.remove(RemedioPersistence.TABLENAME, 
				RemedioPersistence.PRIMARYKEY, remedio.getCodigo());
	}

}
